package com.xl.generator.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.CommonUser;
import com.xl.generator.domain.CompetitionRecord;
import com.xl.generator.domain.CompetitionScheme;
import com.xl.generator.domain.ReviewRecord;
import com.xl.generator.dto.ReviewRecordDetail;
import com.xl.generator.pojo.PageList;
import com.xl.generator.service.CommonUserService;
import com.xl.generator.service.CompetitionRecordService;
import com.xl.generator.service.CompetitionSchemeService;
import com.xl.generator.service.ReviewRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;

import org.apache.ibatis.javassist.compiler.CompileError;
import org.apache.ibatis.javassist.compiler.SyntaxError;
import org.python.util.PythonInterpreter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;

/**
 * 〈〉<br>
 *
 * @author lvln5
 * @Description
 * @date 2022/11/12 19:39
 */
@Api(value = "评审评审表的增删改查")
@RestController
@RequestMapping("reviewRecord")
public class ReviewRecordController {

	@Resource
	private ReviewRecordService reviewRecordService;

	@Resource
	private CommonUserService commonUserService;

	@Resource
	private CompetitionRecordService competitionRecordService;

	@Resource
	private CompetitionSchemeService competitionSchemeService;

	@ApiOperation(value = "分页查询所有评审表", notes="")
	@PostMapping ("list")
	public ActionResult test(@RequestParam("current") Integer current, @RequestParam("Size") Integer size,
							 @RequestBody JSONObject jsonObject){
		LambdaQueryWrapper<ReviewRecord> queryWrapper = new LambdaQueryWrapper<>();
		if (jsonObject.getString("status")!=null&&jsonObject.getString("status").equals("unreadMsg")){
			queryWrapper.isNull(ReviewRecord::getReviewUserId);
		}
		if (jsonObject.getString("status")!=null&&jsonObject.getString("status").equals("readMsg")){
			queryWrapper.isNotNull(ReviewRecord::getReviewUserId);
		}

		IPage<ReviewRecord> choiceQuestion = new Page<>();
		choiceQuestion.setCurrent(current);
		choiceQuestion.setSize(size);

		IPage<ReviewRecord> page = reviewRecordService.page(choiceQuestion,queryWrapper);

		PageList pageList = new PageList<>();
		pageList.setTotal(choiceQuestion.getTotal());
		pageList.setData(page);


		return ActionResultService.callBackResultOK(pageList);
	}

	@ApiOperation(value = "通过RecordReviewId查询", notes="")
	@PostMapping ("getOne")
	public ActionResult getOne(@RequestBody ReviewRecord reviewRecord){

		ReviewRecord entity= reviewRecordService.getById(reviewRecord.getId());
		ReviewRecordDetail reviewRecordDetail = reviewRecordService.resolveReviewRecord(entity);

		String actionResultA = reviewRecordService.executePython(reviewRecordDetail.getReviewRecord().getQuestionA());
		String actionResultB = reviewRecordService.executePython(reviewRecordDetail.getReviewRecord().getQuestionB());
		reviewRecordDetail.setQuestionAReply(actionResultA);
		reviewRecordDetail.setQuestionBReply(actionResultB);
		return ActionResultService.callBackResultOK(reviewRecordDetail);

	}

	/**
	 * @param
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "添加评审", notes="注册成功状态码为o,否侧为-1失败")
	@PostMapping("add")
	public ActionResult register(@RequestBody ReviewRecord reviewRecord){
		boolean save = reviewRecordService.save(reviewRecord);
		if (save){
			return ActionResultService.callBackResultOK("添加成功");
		}
		return ActionResultService.callBackResultOK("添加失败");
	}
	/**
	 * @param
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "删除评审", notes="删除成功状态码为o,否侧为-1失败")
	@PostMapping("del")
	public ActionResult del(@RequestBody ReviewRecord reviewRecordn){
		LambdaQueryWrapper<ReviewRecord> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ReviewRecord::getId,reviewRecordn.getId());
		boolean remove = reviewRecordService.remove(queryWrapper);
		if (remove){
			return	ActionResultService.callBackResultOK("删除成功");
		}
		return ActionResultService.callBackResultFail("删除失败");
	}
	@ApiOperation(value = "更新评审", notes="更新成功状态码为o,否侧为-1失败")
	@PostMapping("update")
	public ActionResult updata(@RequestBody ReviewRecord reviewRecord){
		CompetitionRecord competitionRecord = new CompetitionRecord();
		competitionRecord.setJionTime(reviewRecord.getSubmitTime());
		CommonUser commonUser = commonUserService.getById(reviewRecord.getCommonUserId());
		competitionRecord.setUserAccount(commonUser.getUserAccount());
		competitionRecord.setCompetitionSchemeId(reviewRecord.getCompetitionSchemeId());
		int total=  reviewRecord.getQuestionBScore()+reviewRecord.getQuestionAScore();

		int credit = commonUser.getTotalCredit();
		credit += total;
		commonUser.setTotalCredit(credit);
		CompetitionScheme competitionScheme = competitionSchemeService.getById(reviewRecord.getCompetitionSchemeId());
		if (credit>=120&&commonUser.getDegree()<=competitionScheme.getCompetitionId())
		{
			commonUser.setDegree(competitionScheme.getCompetitionId()+1);
		}
		commonUserService.updateById(commonUser);
		competitionRecord.setPass(total>=120?1:0);
		competitionRecord.setCompetitionScore(total);
		competitionRecord.setSubmitTime(reviewRecord.getSubmitTime());
		competitionRecord.setReviewTime(reviewRecord.getReviewTime());
		competitionRecordService.save(competitionRecord);
		boolean update = reviewRecordService.updateById(reviewRecord);
		if (update){
			return	ActionResultService.callBackResultOK("更新成功");
		}
		return ActionResultService.callBackResultFail("更新失败");
	}

	@ApiOperation(value = "执行python程序", notes="执行python程序")
	@PostMapping("executePython")
	public ActionResult executePython(@RequestBody JSONObject jsonObject) {

		String code = jsonObject.getString("code");
		if (code==null){
			code="";
		}
		ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
		ByteArrayOutputStream errorStream = new ByteArrayOutputStream(1024);
		PythonInterpreter interpreter = new PythonInterpreter();
		interpreter.setOut(baoStream);
		interpreter.setErr(errorStream);
		try{
			//我在这里使用相对路径，注意区分
			interpreter.exec(code);
			String strMsg = baoStream.toString();
			return ActionResultService.callBackResultOK(strMsg);
		}
		catch (Exception e){
			e.printStackTrace();
			return ActionResultService.callBackResultFail(errorStream.toString());
		}

	}


}
